//
//  H5ViewController.swift
//  maopu
//
//  Created by xiongsh on 2017/5/8.
//  Copyright © 2017年 maopu. All rights reserved.
//

import UIKit

class H5ViewController: BaseViewController, UIWebViewDelegate {
    fileprivate var webView: UIWebView!
    fileprivate var activityIndicatorView: UIActivityIndicatorView!
    fileprivate var titleNav: String!
    fileprivate var url: URL!
    init(title: String, url: URL) {
        super.init(nibName: nil, bundle: nil)
        self.titleNav = title
        self.url = url
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initView()
        self.initData()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func initData() {
        
    }
    
    func initView() {
        self.title = self.titleNav
        
        self.webView = UIWebView.init(frame: CGRect.init(x: 0, y: 0, width: AppWidth, height: AppHeight))
        let urlRequest: URLRequest = URLRequest.init(url: self.url)
        self.webView.scalesPageToFit = true
        self.webView.delegate = self
        self.webView.loadRequest(urlRequest as URLRequest)
        self.view.addSubview(self.webView)
        
        self.activityIndicatorView = UIActivityIndicatorView.init(activityIndicatorStyle: .gray)
        self.activityIndicatorView.center = CGPoint.init(x: AppWidth/2, y: AppHeight/2)
        self.view.addSubview(self.activityIndicatorView)
        self.activityIndicatorView.startAnimating()
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    
    func webViewDidFinishLoad(_ webView: UIWebView) {
        self.activityIndicatorView.stopAnimating()
//        self.title = webView stringByEvaluatingJavaScriptFromString:@"document.title"];
        self.title = webView.stringByEvaluatingJavaScript(from: "document.title")
    }
    func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
        self.activityIndicatorView.stopAnimating()
    }
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        return true
    }

}
